package com.itheima;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @ClassName zengServer
 * @Author 16799
 * @Date 2025/10/17 9:31
 * @Version 1.0
 * @Description TODO
 **/
public class zengServer {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(8080);

        while (true){
            Socket socket = ss.accept();
            System.out.println("连接的是： "+socket.getRemoteSocketAddress());

            Thread r = new zengHandler(socket);
            r.start();
        }
    }
}

class zengHandler extends Thread{
    Socket socket;
    public zengHandler(Socket socket){
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            InputStream input = this.socket.getInputStream();
            OutputStream output = this.socket.getOutputStream();
            zengHandle(input,output);
        }catch (Exception e){
            try {
                this.socket.close();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            System.out.println("连接断了");
        }
    }

    private void zengHandle(InputStream input , OutputStream output) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output,StandardCharsets.UTF_8));

        Boolean requestOk = false;
        String first = reader.readLine();
        if (first.startsWith("GET / HTTP/1.")) {
            requestOk = true;
        }

        for (;;){
            String header = reader.readLine();
            if (header.isEmpty()){
                break;
            }
            System.out.println(header);
        }

        System.out.println(requestOk ? "response ok" : "response error");

        if (!requestOk){
            writer.write("HTTP/1.0 404 Not Found\r\n");
            writer.write("Content-Length: 0\r\n");
            writer.write("\r\n");
            writer.flush();
        }else {
            InputStream is = zengServer.class.getClassLoader().getResourceAsStream("html/a.html");
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder data = new StringBuilder();
            String line = null;
            while ((line = br.readLine()) != null){
                data.append(line);
            }
            br.close();
            int length = data.toString().getBytes(StandardCharsets.UTF_8).length;
            writer.write("HTTP/1.1 200 OK\r\n");
            //接着这些键值对类型的就是一个一个的响应头
            writer.write("Connection: keep-alive\r\n");
            writer.write("Content-Type: text/html\r\n");
            writer.write("Content-Length: " + length + "\r\n");
            writer.write("\r\n"); // 空行标识Header和Body的分隔
            //然后是响应体，要和响应体空开一行，响应体存放的就是我们要给浏览器返回的响应数据
            //在本次案例中，这个数据就是我们读取的a.html文件的内容，最终浏览器拿到了这个响应体中的数据内容之后，经过浏览器的渲染解析展示出来一个表格
            writer.write(data.toString());
            writer.flush();
        }
    }
}
